package com.tanhua.autoconfig.template;

import com.baidu.aip.contentcensor.AipContentCensor;
import com.baidu.aip.contentcensor.EImgType;
import com.tanhua.autoconfig.properties.GreenProperties;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class GreenTemplate {

    @Autowired
    private AipContentCensor aipContentCensor;

    public GreenTemplate(GreenProperties properties) {
        log.error(properties.getAppId());
        log.error(properties.getApiKey());
        log.error(properties.getSecretKey());
        this.aipContentCensor = new AipContentCensor(properties.getAppId(), properties.getApiKey(), properties.getSecretKey());
    }
    /**
     * 百度云图片审核
     */
    public Map imageScan(List<String> imageList) {
        Map<String, String> responseMap = new HashMap<>();
        for (String url : imageList) {
            JSONObject response = aipContentCensor.imageCensorUserDefined(url, EImgType.URL, null);
            System.out.println(response);
            if(response.getString("conclusion").equals("不合规")){
                responseMap.put("flag","no");
                JSONArray dataArray = response.getJSONArray("data");
                JSONObject firstDataObject = dataArray.getJSONObject(0);
                String firstMsgValue = firstDataObject.getString("msg");
                responseMap.put("msg",firstMsgValue);
                return responseMap;
            }
        }
        responseMap.put("flag","yes");
        responseMap.put("msg","图片审核通过");
        return responseMap;
    }

    /**
     * 百度云文本审核
     */
    public Map<String,String> greenTextScan(String content){
        JSONObject response = aipContentCensor.textCensorUserDefined(content);
        System.out.println(response);
        Map<String,String> responseMap = new HashMap<>();
        if(response.getString("conclusion").equals("不合规")){
            responseMap.put("flag","no");
            JSONArray dataArray = response.getJSONArray("data");
            JSONObject firstDataObject = dataArray.getJSONObject(0);
            String firstMsgValue = firstDataObject.getString("msg");
            responseMap.put("msg",firstMsgValue);
            return responseMap;
        }
        responseMap.put("flag","yes");
        responseMap.put("msg","文本审核通过");
        return responseMap;
    }

    /**
     * 百度云视频审核
     */
    public Map<String,String> greenVideoScan(String videoUrl){
        //可选参数配置
        HashMap<String, Object> options=new HashMap();
        options.put("rawText","false");
        options.put("split","true");
        int rate=1600;//采样率
        JSONObject response = aipContentCensor.voiceUrlCensorUserDefined("url", rate, "wav", options);
        System.out.println(response);
        Map<String,String> responseMap = new HashMap<>();
        if(response.getString("conclusion").equals("不合规")){
            responseMap.put("flag","no");
            responseMap.put("msg","视频含有违规内容");
            return responseMap;
        }
        responseMap.put("flag","yes");
        responseMap.put("msg","视频审核通过");
        return responseMap;
    }

}